//
//  HZRoomLogic.m
//  Model
//
//  Created by zeb-apple on 2017/5/26.
//  Copyright © 2017年 YDJH. All rights reserved.
//  业务逻辑处理

#import "HZRoomLogic.h"
#import "HZRoom.h"
#import "HZFurniture.h"
#import "HZCommand.h"
#import "HZFurnitureAction.h"
#import "HZOutsideFurnitureAction.h"
#import "BuildingConstructionLogicSystem.h"
#import "HZOutsideWall.h"
#import "HZFurniture_Header.h"

@implementation HZRoomLogic


//获取第一层 第一个房间
+ (HZRoom *)goBackFiristRoomWithBuildingStructure:(NSMutableArray *)buildingStructure {

    HZRoom *firistRoom = nil;
    
    for (NSMutableArray *array in buildingStructure) {
        
        for (HZRoom *room in array) {
            
            if (room.isFirstSurfaceRoom) {
                firistRoom = room;
                break;
            }
            
        }
        if (firistRoom) {
            break;
        }
    }
    if (!firistRoom) {
        NSLog(@"没有第一层 ， 数据初始化错误");
    }
    [HZRoomLogic updataBuildingCurrentIndex:firistRoom.position];
    return firistRoom;
}
//根据家具的状态返回一张图片
+ (UIImage *)goBackImageWithFurnitureState:(NSString *)state {

    UIImage *image = nil;
    
    if ([state isEqualToString:@"close"]) {
        image = [UIImage imageNamed:@"closedoor"];
    }else if ([state isEqualToString:@"open"]) {
        image = [UIImage imageNamed:@"opendoor"];
    }else if ([state isEqualToString:@"lock"]) {
        image = [UIImage imageNamed:@"lockdoor"];
    }else if ([state isEqualToString:@"destory"]) {
        image = [UIImage imageNamed:@"destorydoor"];
    }else if ([state isEqualToString:@"light"]) {
        image = [UIImage imageNamed:@""];
    }else if ([state isEqualToString:@"extinguish"]) {
        image = [UIImage imageNamed:@""];
    }else if ([state isEqualToString:@"normal"]) {
        image = [UIImage imageNamed:@"normal"];
    }
    
    return image;
    
}
//根据指令 执行对应的方法
+ (HZRoom *)performSelectorWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command buildingStructure:(NSMutableArray *)buildingStructure {
    
    HZRoom *room = nil;
    SEL selector = NSSelectorFromString(command.methodName);
//    [self changeFurnitureState:furniture withCommandName:command.commandName];
    HZFurnitureAction *action = [HZFurnitureAction standardHZFurnitureAction];
    if ([action respondsToSelector:selector]) {
        SuppressPerformSelectorLeakWarning(room = [action performSelector:selector withObject:furniture withObject:buildingStructure];);

    }
    return room;
}

+ (HZSandTable *)performSelectorWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command construction:(HZConstruction *)construction {
    
    HZSandTable *sandtable = nil;
    
    SEL selector = NSSelectorFromString(command.methodName);
//    [self changeFurnitureState:furniture withCommandName:command.commandName];
    HZOutsideFurnitureAction *action = [HZOutsideFurnitureAction standardHZFurnitureAction];
    if ([action respondsToSelector:selector]) {
        SuppressPerformSelectorLeakWarning(sandtable = [action performSelector:selector withObject:furniture withObject:construction];);
    }
    return sandtable;
}

//更新所处房间位置
+ (void)updataBuildingCurrentIndex:(NSIndexPath *)indexPath {
    BuildingConstructionLogicSystem *buildingConstructionLogicSystem = [BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem];
    buildingConstructionLogicSystem.currentIndex = indexPath;
}
//返回当前房间的位置
+ (NSIndexPath *)goBackBuildingCurrentIndex {
    BuildingConstructionLogicSystem *buildingConstructionLogicSystem = [BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem];
    return buildingConstructionLogicSystem.currentIndex;
}
//返回建筑轮廓
+ (NSString *)goBackStructure {
    BuildingConstructionLogicSystem *buildingConstructionLogicSystem = [BuildingConstructionLogicSystem standardBuildingConstructionLogicSystem];
    return buildingConstructionLogicSystem.structure;
}
//根据外墙返回 以及攀爬方向返回对应的房间
+ (HZRoom *)goBackRoomWhenOutsideWithOutsideWall:(HZOutsideWall *)outsideWall buildingStructure:(NSMutableArray *)buildingStructure direction:(NSInteger)direction { // 0 上 1 下 2 左 3 右
    
    NSIndexPath *indexpath = outsideWall.position;
    HZRoom *lastroom = buildingStructure[indexpath.section][indexpath.row];
    lastroom.roomState = RoomState_Came;
    HZRoom *room = nil;

    switch (direction) {
        case 0: //上
            room = buildingStructure[indexpath.section + 1][indexpath.row];
            break;
        case 1: //下
            room = buildingStructure[indexpath.section - 1][indexpath.row];
            break;
        case 2: //左
            room = buildingStructure[indexpath.section][indexpath.row - 1];
            break;
        case 3: //右
            room = buildingStructure[indexpath.section][indexpath.row + 1];
            break;
        default:
            break;
    }
    return room;
}
// 根据RoomViewController指令返回对应的操作
+ (SEL)goBackSelectFromRoomViewControllerActionWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    
    if ([furniture.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //门
        if ([command.type rangeOfString:enter_Type].location != NSNotFound) { //切换房间
            return NSSelectorFromString(@"enterRoomWithFurniture:command:");
        }
    }else if ([furniture.furnitureName rangeOfString:Window_Type].location != NSNotFound) { //窗户
        if ([command.type rangeOfString:climb_Type].location != NSNotFound) { //爬窗户
            return NSSelectorFromString(@"climbWindowWithFurniture:command:");
        }
    }else if ([furniture.furnitureName rangeOfString:Staircase_Type].location != NSNotFound) { //楼梯
        if ([command.type rangeOfString:enter_Type].location != NSNotFound) { //切换房间
            return NSSelectorFromString(@"enterRoomWithFurniture:command:");
        }
    }else if ([furniture.furnitureName rangeOfString:Ventilation_Duct_Type].location != NSNotFound) { //通风管道
        if ([command.type rangeOfString:enter_Type].location != NSNotFound) { //进入通风管道
            return NSSelectorFromString(@"enterVentstacksSceneWithFurniture:command:");
        }
    }
    return nil;
}

// 根据HZOutsideRoomViewController指令返回对应的操作
+ (SEL)goBackSelectFromHZOutsideRoomViewControllerActionWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    //当前房间的位置
    NSIndexPath *indexPath = [HZRoomLogic goBackBuildingCurrentIndex];
    
    if ([furniture.furnitureName rangeOfString:Window_Type].location != NSNotFound) { //窗户
        if ([command.type rangeOfString:climb_Type].location != NSNotFound) { //进房间
            return NSSelectorFromString(@"enterRoomWithWindow:command:");
        }
    }else if ([furniture.furnitureType isEqualToString:OutsideUpStairs]) { //上外梯
        
        if ([furniture.indexPath isEqual:indexPath]) { //上外梯
            if ([command.type rangeOfString:climb_Type].location != NSNotFound) { //往上攀爬
                return NSSelectorFromString(@"climbUpWithLabber:command:");
            }
        }else { //下外梯
            if ([command.type rangeOfString:climb_Type].location != NSNotFound) { //往下攀爬
                return NSSelectorFromString(@"climbDownWithLabber:command:");
            }
        }
        
    }else if ([furniture.furnitureName rangeOfString:Door_Type].location != NSNotFound) { //门
        if ([command.type rangeOfString:enter_Type].location != NSNotFound) {
            return NSSelectorFromString(@"enterRoomWithDoor:command:");
        }
    }
    return nil;
}
#pragma mark - 改变家具状态
+ (void)changeFurnitureState:(HZFurniture *)furniture withCommandName:(NSString *)commandName {
    
    //门
    if ([furniture.furnitureName rangeOfString:Door_Type].location != NSNotFound) {
        [self changeDoorFurniture:furniture withCommandName:commandName];
    }
    //窗
    if ([furniture.furnitureName rangeOfString:Window_Type].location != NSNotFound) {
        [self changeWindowFurniture:furniture withCommandName:commandName];
    }
    //柜子，桌子 
    if ([furniture.furnitureType isEqualToString:Cabinet] || [furniture.furnitureType isEqualToString:Table]) {
        [self changeCollectionFurniture:furniture withCommandName:commandName];
    }
    //物品架，吧台，床，破墙
    if ([furniture.furnitureType isEqualToString:GoodsRack] || [furniture.furnitureType isEqualToString:Bar] || [furniture.furnitureType isEqualToString:Bed] || [furniture.furnitureType isEqualToString:BrokenWall]) {
        [self changeGeneralFurniture:furniture withCommandName:commandName];
    }
    //火堆
    if ([furniture.furnitureType isEqualToString:Fire]) {
        [self changeBonfiresFurniture:furniture withCommandName:commandName];
    }
    
    //梯
    if ([furniture.furnitureName rangeOfString:Staircase_Type].location != NSNotFound) {
        [self changeGeneralFurniture:furniture withCommandName:commandName];
    }
    
    //管道
//    if ([furniture.furnitureName rangeOfString:Ventilation_Duct_Type].location != NSNotFound) {
//        [self changeGeneralFurniture:furniture withCommandName:commandName];
//    }
    
}

+ (void)changeDoorFurniture:(HZFurniture *)furniture withCommandName:(NSString *)commandName {
    
    if ([commandName isEqualToString:enter_Type]) {
        //门关闭状态下，进入指令有效
        if ([furniture.furnitureState isEqualToString:close_state]) {
            furniture.furnitureState = open_state;
        }
    }else if ([commandName isEqualToString:close_Type]) {
        //门打开状态下，关闭指令有效
        if ([furniture.furnitureState isEqualToString:open_state]) {
            furniture.furnitureState = close_Type;
        }
    }else if ([commandName isEqualToString:destory_Type]) {
        //任何状态下，有效
        NSLog(@"---此处应该有道具判断---，如果有破坏道具，并且体力足够破坏该强度的家具，即可打开");
        furniture.furnitureState = destory_state;
        
    }else if ([commandName isEqualToString:unlocking_Type]){
        if ([furniture.furnitureState isEqualToString:lock_state]) {
            NSLog(@"---此处应有道具判断---，如果有开锁道具，即可打开");
            furniture.furnitureState = open_state;
        }
    }else {
        NSLog(@"no change");
    }
}

+ (void)changeWindowFurniture:(HZFurniture *)furniture withCommandName:(NSString *)commandName {
    
    if ([commandName isEqualToString:open_Type]) {
        if ([furniture.furnitureState isEqualToString:close_state]) {
            furniture.furnitureState = open_state;
        }
    }else if ([commandName isEqualToString:close_Type]) {
        if ([furniture.furnitureState isEqualToString:open_state]) {
            furniture.furnitureState = close_Type;
        }
    }else if ([commandName isEqualToString:destory_Type]) {
        NSLog(@"---此处应该有道具判断---，如果有破坏道具，并且体力足够破坏该强度的家具，即可打开");
        furniture.furnitureState = destory_state;
    }else {
        NSLog(@"no change");
    }
}
//家具状态类别只有 正常或者破坏两种
+ (void)changeGeneralFurniture:(HZFurniture *)furniture withCommandName:(NSString *)commandName {

    if ([commandName isEqualToString:destory_Type]) {
        NSLog(@"---此处应该有道具判断---，如果有破坏道具，并且体力足够破坏该强度的家具，即可打开");
        furniture.furnitureState = destory_state;
    }else {
        NSLog(@"no change");
    }
}
//收藏类家具 桌子或者柜子
+ (void)changeCollectionFurniture:(HZFurniture *)furniture withCommandName:(NSString *)commandName {
    
    if ([commandName isEqualToString:open_Type]) {
        if ([furniture.furnitureState isEqualToString:close_Type]) {
            furniture.furnitureState = open_state;
        }
    }else if ([commandName isEqualToString:close_Type]) {
        if ([furniture.furnitureState isEqualToString:open_Type]) {
            furniture.furnitureState = close_state;
        }
    }else if ([commandName isEqualToString:destory_Type]) {
        NSLog(@"---此处应该有道具判断---，如果有破坏道具，并且体力足够破坏该强度的家具，即可打开");
        furniture.furnitureState = destory_state;
    }else {
        NSLog(@"no change");
    }
}

//火堆
+ (void)changeBonfiresFurniture:(HZFurniture *)furniture withCommandName:(NSString *)commandName {
    
    if ([commandName isEqualToString:light_Type]) {
        furniture.furnitureState = light_state;
    }else if ([commandName isEqualToString:extinguish_Type]) {
        furniture.furnitureState = extinguish_state;
    }else {
        NSLog(@"no change");
    }
    
}


+ (NSString *)isNullToString:(id)string
{
    if ([string isEqual:@"NULL"] || [string isKindOfClass:[NSNull class]] || [string isEqual:[NSNull null]] || [string isEqual:NULL] || [[string class] isSubclassOfClass:[NSNull class]] || string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@"<null>"] || [string isEqualToString:@"(null)"])
    {
        return @"";
        
        
    }else
    {
        
        return (NSString *)string;
    }
}

@end
